package com.outsourcing.bottle.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.Html;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ext.SatelliteMenu;
import android.view.ext.SatelliteMenu.SateliteClickedListener;
import android.view.ext.SatelliteMenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.db.LoginUserInfoTable;
import com.outsourcing.bottle.db.PushNoticeInfoTable;
import com.outsourcing.bottle.domain.AlbumEntry;
import com.outsourcing.bottle.domain.AlbumListInfo;
import com.outsourcing.bottle.domain.FriendEntry;
import com.outsourcing.bottle.domain.LoginUserInfoEntry;
import com.outsourcing.bottle.domain.PushNoticeInfo;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.ImageLoader;
import com.outsourcing.bottle.util.TextUtil;
import com.outsourcing.bottle.util.UICore;
import com.outsourcing.bottle.widget.ElasticScrollView;
import com.outsourcing.bottle.widget.ElasticScrollView.OnRefreshListener;
import com.outsourcing.bottle.widget.ElasticScrollView.OnScrollChangeBottom;

/**
 * 
 * @author 06peng
 *
 */
public class AlbumsActivity extends BasicActivity implements OnClickListener, Callback {

	private ImageView backView;
	private ImageView addView;
	private TextView titleView;
	private ImageView avatarView;
	private TextView nicknameView;
	private ImageView sexView;
	private TextView memoView;
	private TextView doingView;
	private TextView addressView;
	private GridView gridView;
	private ElasticScrollView elasticScrollView;
	private View contentView;
	
	private int ouid;
	private int count = 10;
	private final int init_album_info = 1;
	private final int error = 2;
	private final int delete_album = 3;
	private final int user_baseinfo_notfull = 4;
	private final int complete_info = 5;
	private final int get_more_album = 6;
	public static final int add_album_requestcode = 7;
	public static final int update_album_requestcode = 8;
	public static final int upload_photo_requestcode = 9;
	
	private Handler handler;
	private List<AlbumEntry> albumList;
	private FriendEntry friendEntry;
	private Bitmap bitmap;
	private int selectedIndex;
	
	private final int menu_uploadphoto = 15;
	private final int menu_exprofile = 14;
	private final int menu_exphoto = 13;
	private final int menu_gainbt = 12;
	private final int menu_sendbt = 11;
	private int page = 1;
	private ImageView homeView;
	private AlbumAdapter adapter;
	
	private LinearLayout bottomLayout;
	private ImageView bottomAvatarView;
	private TextView bottomContentView;
	private TextView btnCount;
//	private ImageView messageView;
	private ImageLoader loader;
	private int newNoticeCount;
	private int newMessageCount;
	private int newBottleFeedCount;
	private int newExchangeFeedCount;
	private int newMaybeKownCount;
	private String message_content;
	private String message_avatar;
	
	private PushBroadcastReceiver receiver;
	private PushNoticeInfo pushInfo;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.album_elastic_scrolll);
		loader = new ImageLoader(this, AppContext.BottleTimelineIcon);
		handler = new Handler(this);
		ouid = getIntent().getIntExtra("ouid", 0);
		initWidget();
		initSateMenu();
		UICore.eventTask(this, this, init_album_info, "init_album_info", null);
	}
	
	private void initWidget() {
		homeView = (ImageView) findViewById(R.id.topbar_home);
		homeView.setOnClickListener(this);
		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		addView = (ImageView) findViewById(R.id.topbar_menu);
		addView.setOnClickListener(this);
		addView.setImageResource(R.drawable.add_white);
		titleView = (TextView) findViewById(R.id.topbar_title);
		titleView.setVisibility(View.VISIBLE);
		bottomLayout = (LinearLayout) findViewById(R.id.notice_bottom_layout);
		bottomLayout.setOnClickListener(this);
		bottomAvatarView = (ImageView) findViewById(R.id.notice_avatar);
		bottomContentView = (TextView) findViewById(R.id.notice_text);
		btnCount = (TextView) findViewById(R.id.notice_number);
//		messageView = (ImageView) findViewById(R.id.notice_message_count);
//		messageView.setOnClickListener(this);
		
		
		elasticScrollView = (ElasticScrollView) findViewById(R.id.refresh_view);
		elasticScrollView.setonRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				page = 1;
				UICore.eventTask(AlbumsActivity.this, AlbumsActivity.this, init_album_info, null, null);
			}
		});
		elasticScrollView.setonScrollChangeListener(new OnScrollChangeBottom() {
			
			@Override
			public void onScrollBottom() {
				page++;
				UICore.eventTask(AlbumsActivity.this, AlbumsActivity.this, get_more_album, null, null);
			}
		});
		contentView = LayoutInflater.from(this).inflate(R.layout.user_albums, null);
		elasticScrollView.addChild(contentView);
		
		avatarView = (ImageView) contentView.findViewById(R.id.user_album_avatar);
		nicknameView = (TextView) contentView.findViewById(R.id.user_album_nickname);
		sexView = (ImageView) contentView.findViewById(R.id.user_album_sex);
		memoView = (TextView) contentView.findViewById(R.id.user_album_memo);
		doingView = (TextView) contentView.findViewById(R.id.user_album_doing);
		addressView = (TextView) contentView.findViewById(R.id.user_album_address);
		gridView = (GridView) contentView.findViewById(R.id.user_album_grid);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				selectedIndex = arg2;
				if (ouid == AppContext.getInstance().getLogin_uid()
						&& albumList.get(selectedIndex).getAlbum_picnum() == 0) {
					doChooseAction();
				} else {
					Intent intent = new Intent(AlbumsActivity.this, PhotosActivity.class);
					intent.putExtra("ouid", ouid);
					intent.putExtra("albumid", albumList.get(selectedIndex).getAlbum_id());
					startActivity(intent);
				}
			}
		});
	}
	
	private void updateUI() {
		homeView.setVisibility(View.VISIBLE);
		addView.setVisibility(View.VISIBLE);
		findViewById(R.id.album_scrollview_layout).setVisibility(View.VISIBLE);
		if (friendEntry != null) {
			nicknameView.setText(friendEntry.getNickname());
			if (friendEntry.getSex() == 1) {
				sexView.setBackgroundResource(R.drawable.male);
			} else if (friendEntry.getSex() == 2) {
				sexView.setBackgroundResource(R.drawable.female);
			}
			if (TextUtil.notEmpty(friendEntry.getMemo())) {
				memoView.setText("-" + friendEntry.getMemo());
				memoView.setVisibility(View.VISIBLE);
			} else {
				memoView.setVisibility(View.GONE);
			}
			String html = "<font color=black>"+getString(R.string.user_info_doing)+"</font>";
			doingView.setText(Html.fromHtml(html + friendEntry.getDoing()));
			if (TextUtil.notEmpty(friendEntry.getCity()  )&&  TextUtil.notEmpty(friendEntry.getCountry())) {
				addressView.setVisibility(View.VISIBLE);
				if (AppContext.language == 1) {
					addressView.setText(getString(R.string.user_from) + friendEntry.getCountry() + " " + friendEntry.getCity());
				} else {
					addressView.setText(getString(R.string.user_from) + friendEntry.getCity() + " " + friendEntry.getCountry());
				}
			} else {
				addressView.setVisibility(View.GONE);
			}
			if (bitmap != null) {
				avatarView.setImageBitmap(bitmap);
			}
			titleView.setText(friendEntry.getNickname() + getString(R.string.album_who));
		}
		if (ouid == AppContext.getInstance().getLogin_uid()) {
			addView.setVisibility(View.VISIBLE);
		} else {
			addView.setVisibility(View.GONE);
		}
		if (albumList != null && !albumList.isEmpty()) {
			adapter = new AlbumAdapter();
			gridView.setAdapter(adapter);
		}
	}
	
	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case init_album_info:
			updateUI();
			if (adapter != null && adapter.getCount() >= count) {
				elasticScrollView.setMoreCount(true);
			} else {
				elasticScrollView.setMoreCount(false);
			}
			elasticScrollView.onRefreshComplete();
			pushNoticeUpdateUI();
			
			receiver = new PushBroadcastReceiver();
			IntentFilter filter = new IntentFilter();
			filter.addAction("push");
			registerReceiver(receiver, filter);
			break;
		case error:
			onToast((String) msg.obj);
			break;
		case delete_album:
			onToast((String) msg.obj);
			UICore.eventTask(this, this, init_album_info, "init_album_info", null);
			break;
		case user_baseinfo_notfull:
			showChoseMes(getString(R.string.exchange_complete_profile_tips), complete_info);
			break;
		case get_more_album:
			adapter.notifyDataSetChanged();
			elasticScrollView.onScrollChangeComplete();
			break;
		default:
			break;
		}
		return false;
	}

	
	@Override
	public void onClick(View v) {
		if (v == backView) {
			finish();
		} else if (v == homeView) {
			Intent intent = new Intent(this, HomeActivity.class);
			intent.putExtra("currentItem", 1);
			startActivity(intent);
		}else if (v == addView) {
			Intent intent = new Intent(this, ExpandEditActivity.class);
			Bundle extras = new Bundle();
			extras.putString("from_type", AppContext.ADD_ALBUM);
			intent.putExtras(extras);
			startActivityForResult(intent, add_album_requestcode);
		} 
//		else if (v == messageView) {
//			doMessageAction();
//		} 
		else if (v == bottomLayout) {
			doFeedsAction();
		}
	}
	
	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case init_album_info:
			initAlbums();
			break;
		case delete_album:
			deleteAlbum();
			break;
		case get_more_album:
			getMoreAlbums();
		default:
			break;
		}
	}

	private void initAlbums() {
		StringBuffer url = new StringBuffer(UrlConfig.get_albums_list);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		url.append("&count=" + count);
		url.append("&page=" + page);
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result == null) {
				return;
			}
			Gson gson = new Gson();
			AlbumListInfo info = gson.fromJson(result, AlbumListInfo.class);
			if (info.getResults().getAuthok() == 0) {
				Message msg = Message.obtain(handler, error, info.getResults().getErrmsg());
				handler.sendMessage(msg);
			} else {
				PushNoticeInfoTable table = new PushNoticeInfoTable();
				pushInfo = table.getPushNoticeInfo(AppContext.getInstance().getLogin_uid());
				if (pushInfo != null) {
					newBottleFeedCount = pushInfo.getNew_btfeed_count();
					newExchangeFeedCount = pushInfo.getNew_exfeed_count();
					newMessageCount = pushInfo.getNew_message_count();
					newNoticeCount = pushInfo.getNew_notice_count();
					newMaybeKownCount = pushInfo.getNew_maybeknow_count();
					message_content = pushInfo.getMessage_content();
					message_avatar = pushInfo.getMessage_avatar();
				}
				albumList = info.getAlbums_list();
				friendEntry = info.getUser_info();
				bitmap = HttpUtils.getBitmapFromUrl(AlbumsActivity.this, friendEntry.getAvatar());
				handler.sendEmptyMessage(init_album_info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void getMoreAlbums() {
		StringBuffer url = new StringBuffer(UrlConfig.get_albums_list);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		url.append("&count=" + count);
		url.append("&page=" + page);
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result == null) {
				return;
			}
			Gson gson = new Gson();
			AlbumListInfo info = gson.fromJson(result, AlbumListInfo.class);
			if (info.getResults().getAuthok() == 0) {
				Message msg = Message.obtain(handler, error, info.getResults().getErrmsg());
				handler.sendMessage(msg);
			} else {
				List<AlbumEntry> moreAlbumList = info.getAlbums_list();
				if (moreAlbumList != null && !moreAlbumList.isEmpty()) {
					albumList.addAll(moreAlbumList);
				}
				handler.sendEmptyMessage(get_more_album);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void deleteAlbum() {
		String url = UrlConfig.delete_album;
		HashMap<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("login_uid", String.valueOf(AppContext.getInstance().getLogin_uid()));
		paramsMap.put("login_token", AppContext.getInstance().getLogin_token());
		paramsMap.put("albumid", String.valueOf(albumList.get(selectedIndex).getAlbum_id()));
		try {
			String result = HttpUtils.doPost(this, url, paramsMap);
			if (result != null) {
				JSONObject object = new JSONObject(result);
	        	JSONObject resultObj = object.getJSONObject("results");
	        	int success = resultObj.getInt("success");
	        	if (success == 0) {
	        		String errmsg = resultObj.getString("errmsg");
	        		Message msg = Message.obtain(handler, error, errmsg);
	        		handler.sendMessage(msg);
	        	} else {
	        		String successmsg = resultObj.getString("successmsg");
					Message msg = Message.obtain(handler, delete_album, successmsg);
					handler.sendMessage(msg);
	        	}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public class AlbumAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return albumList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return albumList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ViewHolder holder;
			if (arg1 == null) {
				arg1 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.user_album_item, null);
				holder = new ViewHolder();
				holder.image = (ImageView) arg1.findViewById(R.id.album_image);
				holder.title = (TextView) arg1.findViewById(R.id.album_title);
				holder.count = (TextView) arg1.findViewById(R.id.album_count);
				arg1.setTag(holder);
			} else {
				holder = (ViewHolder) arg1.getTag();
			}
			AlbumEntry entry = albumList.get(arg0);
			if (entry != null) {
				loader.DisplayImage(entry.getAlbum_cover(), holder.image, R.drawable.album_nophoto);
				holder.title.setText(entry.getAlbum_title());
				holder.count.setText(entry.getAlbum_picnum() + "");
			}
			return arg1;
		}
	}
	
	public static class ViewHolder {
		TextView title;
		TextView count;
		ImageView image;
	}
	
	private void doChooseAction() {
		final Context dialogContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);
		String cancel = getString(R.string.bt_title_back);
		String[] choices;
		choices = new String[3];
		choices[0] = getString(R.string.album_upload_photo);
		choices[1] = getString(R.string.album_edit); 
		choices[2] = getString(R.string.album_delete); 
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						switch (which) {
						case 0:
							Intent intent = new Intent(AlbumsActivity.this, ExpandEditActivity.class);
							Bundle extras = new Bundle();
							extras.putString("from_type", AppContext.UPLOAD_PHOTO);
							extras.putInt("albumid", albumList.get(selectedIndex).getAlbum_id());
							extras.putString("upload_from", "album");
							intent.putExtras(extras);
							startActivityForResult(intent, upload_photo_requestcode);
							break;
						case 1:
							Intent intent2 = new Intent(AlbumsActivity.this, ExpandEditActivity.class);
							Bundle extras2 = new Bundle();
							extras2.putString("from_type", AppContext.EDIT_ALBUM);
							extras2.putInt("albumid", albumList.get(selectedIndex).getAlbum_id());
							extras2.putString("albumname", albumList.get(selectedIndex).getAlbum_title());
							extras2.putString("album_from", "album");
							intent2.putExtras(extras2);
							startActivityForResult(intent2, update_album_requestcode);
							break;
						case 2:
							showChoseMes(getString(R.string.album_delete_confirm), delete_album);
							break;
						}
					}
				});
		builder.setNegativeButton(cancel,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}

				});
		builder.create().show();
	}
	
	SatelliteMenu menu;
	private void initSateMenu() {
		if (menu == null) {
			LinearLayout layout = (LinearLayout) findViewById(R.id.menu_layout);
			menu = new SatelliteMenu(this);
//			menu.setExpandDuration(500);
			if (CanvasWidth >= 480) {
				menu.setSatelliteDistance(200);
			} else {
				menu.setSatelliteDistance(100);
			}
			menu.setTotalSpacingDegree(90.0f);
			menu.setCloseItemsOnClick(true);
			layout.addView(menu);
			
			List<SatelliteMenuItem> items = new ArrayList<SatelliteMenuItem>();
			items.add(new SatelliteMenuItem(menu_exprofile, R.drawable.menu_exprofile));
			items.add(new SatelliteMenuItem(menu_exphoto, R.drawable.menu_exphoto));
			items.add(new SatelliteMenuItem(menu_gainbt, R.drawable.menu_gainbt));
			items.add(new SatelliteMenuItem(menu_sendbt, R.drawable.menu_sendbt));
			items.add(new SatelliteMenuItem(menu_uploadphoto, R.drawable.menu_uploadphoto));
			menu.addItems(items);
		}
		menu.setOnItemClickedListener(new SateliteClickedListener() {

			public void eventOccured(int id) {
				switch (id) {
				case menu_sendbt: //扔瓶子
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent throwIntent = new Intent(AlbumsActivity.this, ChooseBottleActivity.class);
								throwIntent.putExtra("type", "throw");
								startActivity(throwIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_gainbt: //捞瓶子
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent tryIntent = new Intent(AlbumsActivity.this, ChooseBottleActivity.class);
								tryIntent.putExtra("type", "try");
								startActivity(tryIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_exphoto:  //交换照片
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent exchangeInfoIntent = new Intent(AlbumsActivity.this, ChooseFriendActivity.class);
								exchangeInfoIntent.putExtra("choose_friend_type", AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE);
								startActivity(exchangeInfoIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_exprofile: //交换资料
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								LoginUserInfoTable userTable = new LoginUserInfoTable();
								LoginUserInfoEntry userEntry = userTable.getLoginUserInfo(AppContext.getInstance().getLogin_uid());
								if (userEntry.getBasicinfo_fullfill() == 0) {
									handler.sendEmptyMessage(user_baseinfo_notfull);
								} else {
									Intent exchangeFileIntent = new Intent(AlbumsActivity.this, ChooseFriendActivity.class);
									exchangeFileIntent.putExtra("choose_friend_type", AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE);
									startActivity(exchangeFileIntent);
								}
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_uploadphoto:
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent intent = new Intent(AlbumsActivity.this, ExpandEditActivity.class);
								Bundle extras = new Bundle();
								extras.putString("from_type", AppContext.UPLOAD_PHOTO);
								extras.putInt("albumid", 0);
								extras.putInt("ouid", AppContext.getInstance().getLogin_uid());
								intent.putExtras(extras);
								startActivity(intent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				default:
					break;
				}
			}
		});
	}
	
	@Override
	public void sysMesPositiveButtonEvent(int what) {
		if (what == complete_info) {
			Intent intent = new Intent(this, SettingActivity.class);
			startActivity(intent);
		} else if (what == delete_album) {
			UICore.eventTask(this, this, delete_album, "delete_album", null);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == add_album_requestcode) {
				UICore.eventTask(this, this, init_album_info, "init_album_info", null);
			} else if (requestCode == update_album_requestcode) {
				UICore.eventTask(this, this, init_album_info, "init_album_info", null);
			} else if (requestCode == upload_photo_requestcode) {
				UICore.eventTask(this, this, init_album_info, "init_album_info", null);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	public void doMessageAction() {
		
		final Context dialogContext = new ContextThemeWrapper(this,
				android.R.style.Theme_Light);
		String[] choices = new String[2];
		if (newNoticeCount > 0) {
			choices[0] = newNoticeCount + getString(R.string.bottle_txt_notice_list);
		} else {
			choices[0] = getString(R.string.bottle_txt_notice_list);
		}
		if (newMessageCount > 0) {
			choices[1] = newMessageCount + getString(R.string.bottle_txt_message_list);
		} else {
			choices[1] = getString(R.string.bottle_txt_message_list);
		}
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (which == 0) {
							Intent intent = new Intent(AlbumsActivity.this, NoticeListActivity.class);
							startActivity(intent);
						} else {
							Intent intent = new Intent(AlbumsActivity.this, MessageListActivity.class);
							startActivity(intent);
						}
					}
				});
		builder.create().show();
	}
	
	public void doFeedsAction() {
		final Context dialogContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);
		final List<String> choices = new ArrayList<String>(); 
		if (newBottleFeedCount > 0) {
			choices.add(newBottleFeedCount + getString(R.string.bottle_txt_new_bottle_feed));
		}
		if (newExchangeFeedCount > 0) {
			choices.add(newExchangeFeedCount + getString(R.string.bottle_txt_new_exchange_feed));
		}
		if (newNoticeCount > 0) {
			choices.add(newNoticeCount + getString(R.string.bottle_txt_notice_list));
		}
		if (newMessageCount > 0) {
			choices.add(newMessageCount + getString(R.string.bottle_txt_message_list));
		}
		if (newMaybeKownCount > 0) {
			choices.add(newMaybeKownCount + getString(R.string.bottle_txt_new_maybe_kown));
		}
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext, android.R.layout.simple_list_item_1, choices);
		final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_bottle_feed)) != -1) {
							Intent intent = new Intent(AlbumsActivity.this, HomeActivity.class);
							AppContext.getInstance().setCurrentItem(1);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_exchange_feed)) != -1) {
							Intent intent = new Intent(AlbumsActivity.this, HomeActivity.class);
							AppContext.getInstance().setCurrentItem(0);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_notice_list)) != -1) {
							Intent intent = new Intent(AlbumsActivity.this, NoticeListActivity.class);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_message_list)) != -1) {
							Intent intent = new Intent(AlbumsActivity.this, MessageListActivity.class);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_maybe_kown)) != -1) {
							Intent intent = new Intent(AlbumsActivity.this, HomeActivity.class);
							AppContext.getInstance().setCurrentItem(2);
							AppContext.getInstance().setGotoMaybeKown(true);
							startActivity(intent);
						}
					}
				});
		builder.create().show();
	}
	
	public void pushNoticeUpdateUI() {
		if (newBottleFeedCount + newExchangeFeedCount + newMessageCount + newNoticeCount + newMaybeKownCount > 0) {
			bottomLayout.setVisibility(View.VISIBLE);
			bottomContentView.setText(message_content);
			btnCount.setText(newBottleFeedCount + newExchangeFeedCount + newMessageCount + newNoticeCount + newMaybeKownCount  + "");
			loader.DisplayImage(message_avatar, bottomAvatarView, R.drawable.avatar_default_big);
		} else {
			bottomLayout.setVisibility(View.GONE);
		}
	}
	
	public class PushBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				newBottleFeedCount = bundle.getInt("bottle_feed_count");
				newExchangeFeedCount = bundle.getInt("exchange_feed_count");
				newMessageCount = bundle.getInt("message_count");
				newNoticeCount = bundle.getInt("notice_count");
				message_content = bundle.getString("message_content");
				message_avatar = bundle.getString("message_avatar");
				newMaybeKownCount = bundle.getInt("maybe_kown_count");
				pushNoticeUpdateUI();
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		try {
			if (receiver != null) {
				unregisterReceiver(receiver);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
